package ConexaoDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import Entidade.Grupo;

public class GrupoDao {
	private Connection connection;

	public GrupoDao() {
		this.connection = new ConnectionFactory().getConnection();
	}

	public void adiciona(Grupo grupo) {
		String sql = "insert into grupo" + "(nome)" + " values(?)";

		try {

			PreparedStatement stmt = connection.prepareStatement(sql);

			stmt.setString(1, grupo.getNome());
			stmt.execute();
			stmt.close();

		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	public boolean editar(Grupo grupo) {

		String sql = "update grupo set nome=? where id=" + grupo.getIdgrupo();
		try {
			PreparedStatement stmt = connection.prepareStatement(sql);
			stmt.setString(1, grupo.getNome());

			stmt.execute();
			stmt.close();
			return true;
		} catch (SQLException e) {
			throw new RuntimeException(e);

		}
	}

	public boolean delete(Grupo grupo) {
		try {

			PreparedStatement stm = connection
					.prepareStatement("DELETE FROM grupo WHERE id="
							+ grupo.getIdgrupo());
			stm.execute();
			stm.close();
			return true;
		} catch (Exception x) {
			return false;
		}
	}

	public ResultSet selectAll() {
		try {
			PreparedStatement stm = connection
					.prepareStatement("SELECT * FROM grupo");
			ResultSet result = stm.executeQuery();

			return result;
		} catch (Exception x) {
			return null;
		}
	}
}
